<?php
if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' );

if ( ! nv_function_exists( 'nv_albums_blocks' ) )
{
    function nv_block_config_albums_blocks ( $module, $data_block, $lang_block )
    {
        global $site_mods;
        $html = "";
        $html .= "<tr>";
        $html .= "	<td>numrow</td>";
        $html .= "	<td><input type=\"text\" name=\"config_numrow\" size=\"5\" value=\"" . $data_block['numrow'] . "\"/></td>";
        $html .= "</tr>";
		$html .= "<tr>";
		$html .= "<tr>";
        $html .= "	<td>width</td>";
        $html .= "	<td><input type=\"text\" name=\"config_width\" size=\"5\" value=\"" . $data_block['width'] . "\"/></td>";
        $html .= "</tr>";
		$html .= "<tr>";
		$html .= "<tr>";
        $html .= "	<td>height</td>";
        $html .= "	<td><input type=\"text\" name=\"config_height\" size=\"5\" value=\"" . $data_block['height'] . "\"/></td>";
        $html .= "</tr>";
		$html .= "<tr>";
		$html .= "	<td>Style</td>";
        $html .= "	<td><select name=\"config_style\">\n";
		$sel = ($data_block['style'] == 1) ? ' selected' : '';
		$html .= "<option value=\"1\" " . $sel . ">flash_albums</option>\n";
        $sel = ($data_block['style'] == 2) ? ' selected' : '';
		$html .= "<option value=\"2\" " . $sel . ">slide_albums</option>\n";
         $sel = ($data_block['style'] == 3) ? ' selected' : '';
		$html .= "<option value=\"3\" " . $sel . ">list_albums</option>\n";
        $html .= "	</select></td>\n";
        $html .= "</tr>";
        return $html;
    }
	
	function nv_block_config_albums_blocks_submit ( $module, $lang_block )
    {
        global $nv_Request;
        $return = array();
        $return['error'] = array();
        $return['config'] = array();
        $return['config']['style'] = $nv_Request->get_int( 'config_style', 'post', 0 );
        $return['config']['numrow'] = $nv_Request->get_int( 'config_numrow', 'post', 0 );
        $return['config']['width'] = $nv_Request->get_int( 'config_width', 'post', 0 );
        $return['config']['height'] = $nv_Request->get_int( 'config_height', 'post', 0 );
        return $return;
    }
	
	function nv_albums_blocks ( $block_config )
	{
		global  $global_config, $db, $module_info,  $site_mods, $my_head;
		
		if($block_config['style'] == 1)
		{
			$xtpl = new XTemplate( "global.flash_albums.tpl", NV_ROOTDIR . "/themes/" . $global_config['site_theme'] . "/modules/albums/");
			$module = $block_config['module'];
			$xtpl->assign( 'NV_BASE_SITEURL', NV_BASE_SITEURL );
			$xtpl->assign( 'TEMPLATE', $module_info['template'] );
			$xtpl->assign( 'MODULE', $module );
			$xtpl->assign( 'width', $block_config['width'] );
			$xtpl->assign( 'height', $block_config['height'] );
			
			$sql = "SELECT * FROM `" . NV_PREFIXLANG . "_". $site_mods[$module]['module_data']. "_picture` ORDER BY `pictureid` DESC LIMIT 0,".$block_config['numrow']."";
			$reslastest = $db->sql_query( $sql );
			$i = 1;
			while($row = $db->sql_fetchrow($reslastest)){

				$row ['id'] = $i;
				if(!nv_is_url($row['path']))
				$row['path'] = NV_BASE_SITEURL . NV_UPLOADS_DIR . "/" . $module."/" . $row['path'];
				$ex = explode('.',$row['path']);
				$dem = count($ex);
				if($ex[$dem-1]=="jpg")
				{
					$i++;
					$xtpl->assign ( 'loop', $row );
					$xtpl->parse( 'main.loop' );
				}
				
			}
			$xtpl->parse( 'main' );
			return $xtpl->text( 'main' );
		}
		elseif($block_config['style'] == 2)
		{
			$xtpl = new XTemplate( "global.slide_albums.tpl", NV_ROOTDIR . "/themes/" . $global_config['site_theme'] . "/modules/albums/");
			$module = $block_config['module'];
			$xtpl->assign( 'NV_BASE_SITEURL', NV_BASE_SITEURL );
			$xtpl->assign( 'TEMPLATE', $module_info['template'] );
			$xtpl->assign( 'MODULE', $module );
			$xtpl->assign( 'width', $block_config['width'] );
			$xtpl->assign( 'height', $block_config['height'] );
			$my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "themes/" . $module_info['template'] . "/images/albums/album.css\" />\n";
			
			$sql = "SELECT * FROM `" . NV_PREFIXLANG . "_". $site_mods[$module]['module_data']. "_picture` ORDER BY `pictureid` DESC LIMIT 0,".$block_config['numrow']."";
			$reslastest = $db->sql_query( $sql );
			$i = 1;
			while($row = $db->sql_fetchrow($reslastest)){

				$row ['id'] = $i;
				if(!nv_is_url($row['path']))
				$row['path'] = NV_BASE_SITEURL . NV_UPLOADS_DIR . "/" . $module."/" . $row['path'];
				$row['thumb_name'] = NV_BASE_SITEURL . NV_UPLOADS_DIR . "/" . $module."/" . $row['thumb_name'];
				
				$i ++;
				$xtpl->assign ( 'loop', $row );
				$xtpl->parse( 'main.loop' );
			}
			$xtpl->parse( 'main' );
			return $xtpl->text( 'main' );
		}
		elseif($block_config['style'] == 3)
		{
			$xtpl = new XTemplate( "global.list_albums.tpl", NV_ROOTDIR . "/themes/" . $global_config['site_theme'] . "/modules/albums/");
			$module = $block_config['module'];
			$xtpl->assign( 'width', $block_config['width'] );
			$xtpl->assign( 'width_img', $block_config['width']/3 );
			
			//$xtpl->assign( 'height', $block_config['height'] );
			$xtpl->assign( 'NV_BASE_SITEURL', NV_BASE_SITEURL );
			$xtpl->assign( 'TEMPLATE', $module_info['template'] );
			$xtpl->assign( 'MODULE', $module );
			$my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "themes/" . $module_info['template'] . "/images/albums/album.css\" />\n";
    
			$sql = "SELECT `albumid`,`name`,`path_img`,`num_photo`,`num_view`, `alias` FROM `" . NV_PREFIXLANG . "_". $site_mods[$module]['module_data']. "_album` ORDER BY `weight` ASC LIMIT 0,".$block_config['numrow']."";
			$reslastest = $db->sql_query( $sql );
			while($row = $db->sql_fetchrow($reslastest)){
				
				//$row['url_img'] = NV_BASE_SITEURL . NV_UPLOADS_DIR . "/albums/" . $path_img;
				$row['url_link'] = NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $module."&amp;" . NV_OP_VARIABLE . "=view/" . $row['albumid'] . "/" .$row['alias'];
				if(!nv_is_url($row['path_img']))
				$row['path_img'] = NV_BASE_SITEURL . NV_UPLOADS_DIR . "/" . $module."/" . $row['path_img'];
				
				
				$xtpl->assign ( 'loop', $row );
				$xtpl->parse( 'main.loop' );
			}
			$link = NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=albums";
			$xtpl->assign ( 'link', $link );	
			$xtpl->parse( 'main' );
			return $xtpl->text( 'main' );
		}
		
	}
}

if ( defined( 'NV_SYSTEM' ) )
{
    global $site_mods;
    $module = $block_config['module'];
    if ( isset( $site_mods[$module] ) )
    {
        $content = nv_albums_blocks( $block_config );
    }
}
?>